﻿using Nelibur.ObjectMapper;
using Nelibur.ObjectMapper.Bindings;
using System;

namespace CommonNet48.ObjMapper
{
    public static class TinyMapperEx
    {
        /// <summary>
        /// 类型复制(独立新对象)
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="source"></param>
        /// <param name="mapOpt">映射配置</param>
        /// <returns></returns>
        public static TTarget CopyTo<TSource, TTarget>(this TSource source, Action<IBindingConfig<TSource, TTarget>> mapOpt = null)
        {
            if (!TinyMapper.BindingExists<TSource, TTarget>())
            {
                if (mapOpt == null)
                {
                    TinyMapper.Bind<TSource, TTarget>();
                }
                else
                {
                    TinyMapper.Bind(mapOpt);
                }
            }
            return TinyMapper.Map<TSource, TTarget>(source);
        }
    }
}
